#!/usr/bin/python3
from flask import Flask, request
import time
app = Flask(__name__)

@app.route('/status', methods=['GET'])
def get_status():
    id = request.args.get('id')
    if not is_integer(id):return 'Invalid'
    if id:
        # 在这里可以进行对 id 的判断和处理
        command = f'pvesh get nodes/pve/qemu/{id}/status/current -o json'
        print(f"command: {command}")
        result = execute_shell_command(command)
        return result
    else:
        return 'Invalid id'
def is_integer(text):
    try:
        int(text)
        return True
    except ValueError:
        return False

last_command_time={}
last_command_result={}
def execute_shell_command(command):
    nowTime=int(time.time())
    if command in last_command_time and (last_command_time[command] is not None) and (nowTime - last_command_time[command]<60):
       return last_command_result[command]
    else:
       result = execute_shell_command_a(command)
       last_command_result[command]=result
       last_command_time[command]=nowTime
       return result
def execute_shell_command_a(command):
    import subprocess
    try:
        output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
        return output
    except subprocess.CalledProcessError as e:
        return f'Error executing command: {e.output}'

if __name__ == '__main__':
    app.run(debug=False,port=12000,host='0.0.0.0')
